package com.example.lean;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    private String cityName;        //需要获取数据的城市名
    private String cityCode;        //需要获取数据的城市代码
    private List<Fragment> fragList; //ViewPager     各页面的Fragment
    private String result = "";      //API返回的结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取初始化的城市，选择城市后会保存到数据库，下次打开程序会自动显示
        getInitCity();
        //更新天气数据的getWeatherSta()方法
        getWeatherInfo();
    }

    //获取初始化的城市，选择城市后会保存到数据库，下次打开程序会自动显示
    public void getInitCity() {
        SQLiteDatabase db;      //数据库
        MyDBHelper myDBHelper;  //SQLiteOpenHelper
        //数据库操作
        myDBHelper = new MyDBHelper(this, "my.db", null, 1);
        db = myDBHelper.getWritableDatabase();
        //数据库游标
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        //如果数据库为空时先插入一条默认数据 默认数据cityName=北京 cityCode=101010100
        if (cursor.moveToFirst() == false) {
            ContentValues values = new ContentValues();
            values.put("cityCode", "101010100");
            values.put("cityName", "北京");
            db.insert("user", null, values);
            cityName = "北京";
            cityCode = "101010100";
        } else {
            //如果数据库不为空。从数据库获取cityName,cityCode并赋值到cityName变量和cityCode变量
            cityName = cursor.getString(cursor.getColumnIndex("cityName"));
            cityCode = cursor.getString(cursor.getColumnIndex("cityCode"));
        }
    }

    //调用互联网API获取天气信息，在Android 4.0以上，网络连接不能放在主线程上，不然就会报错android.os.NetworkOnMainThreadException
    //Android高版本联网失败报错:Cleartext HTTP traffic to xxx not permitted解决方法 参考：https://blog.csdn.net/gengkui9897/article/details/82863966
    public void getWeatherInfo() {
        Thread t = new Thread() {
            String path = "http://wthrcdn.etouch.cn/weather_mini?citykey=" + cityCode;

            @Override
            public void run() {
                //使用网址构造url
                URL url;
                try {
                    url = new URL(path);
                    //获取连接对象，做设置
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(8000);
                    conn.setReadTimeout(8000);
                    //发送请求，获取响应码
                    if (conn.getResponseCode() == 200) {
                        //获取服务器返回的输入流
                        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                        StringBuilder sb = new StringBuilder();
                        for (int c; (c = in.read()) >= 0; ) {
                            sb.append((char) c);
                        }
                        //发送消息至消息队列，主线程会执行handleMessage
                        Message msg = handler.obtainMessage();
                        msg.obj = sb.toString();
                        in.close();
                        result = sb.toString();
                        handler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            //handler消息队列中有数据会执行以下两行代码
            //改方法用于UI的更新
            setPagerView();
            Toast.makeText(MainActivity.this, "数据获取成功", Toast.LENGTH_SHORT).show();
        }
    };

    public void setPagerView() {
        JSONObject jsonObject;  //用于API的JSON数据操作的JSONObject成员变量
        JSONArray jsonArray;    //用于API的JSON数据操作的JSONArray成员变量
        JSONObject jsonObject2; //用于操作imagecode.json的JSONObject成员变量
        JSONArray jsonArray2;   //用于操作imagecode.json的JSONArray成员变量
        //获取天气类型对应的图片
        String[] arry_data1 = null;    //用于临时保存imagecode.json的天气类型与arry_data2一一对应
        String[] arry_data2 = null;    //用于临时保存imagecode.json的图片ID与arry_data1一一对应
        //将imagecode.json文件中。天气类型和天气图片名分别绑定到arry_data1，arry_data2
        try {
            InputStreamReader isr = new InputStreamReader(getAssets().open("imagecode.json"), "UTF-8");
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
            jsonObject2 = new JSONObject(stringBuilder.toString());
            jsonArray2 = jsonObject2.getJSONArray("img");
            arry_data1 = new String[jsonArray2.length()];
            arry_data2 = new String[jsonArray2.length()];
            for (int i = 0; i < jsonArray2.length(); i++) {
                jsonObject2 = jsonArray2.getJSONObject(i);
                arry_data1[i] = jsonObject2.getString("type");
                arry_data2[i] = jsonObject2.getString("code");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //建立五个 Bundle 用于传递不同日期的天气数据到不同的Fragment 五天的天气数据传到五个Fragment页面
        Bundle b1 = new Bundle();
        Bundle b2 = new Bundle();
        Bundle b3 = new Bundle();
        Bundle b4 = new Bundle();
        Bundle b5 = new Bundle();
        String[] weatherInfo;   //用于保存各个界面的天气信息 weatherInfo[0]对应第一个界面weatherInfo[1]对应第二个界面
        //天气JSON数据解析并将不同的页面的天气信息保存到String weatherInfo[i]里
        //weatherInfo[i]=11日星期三,高温 23℃,<![CDATA[<3级]]>,低温 12℃,无持续风向,小雨,拉萨,13
        try {
            jsonObject = new JSONObject(result);
            jsonObject = jsonObject.getJSONObject("data");
            jsonArray = jsonObject.getJSONArray("forecast");
            weatherInfo = new String[5];
            for (int i = 0; i < 5; i++) {
                jsonObject = jsonArray.getJSONObject(i);
                weatherInfo[i] = jsonObject.getString("date") + ",";
                weatherInfo[i] += jsonObject.getString("high") + ",";
                weatherInfo[i] += jsonObject.getString("fengli") + ",";
                weatherInfo[i] += jsonObject.getString("low") + ",";
                weatherInfo[i] += jsonObject.getString("fengxiang") + ",";
                String type = jsonObject.getString("type");
                weatherInfo[i] += type + ",";
                weatherInfo[i] += cityName + ",";
                String imageCode = "99";
                for (int j = 0; j < arry_data1.length; j++) {
                    if (type.equals(arry_data1[j])) {
                        imageCode = arry_data2[j];
                        break;
                    }
                }
                weatherInfo[i] += imageCode;
            }
            b1.putString("weatherInfo", weatherInfo[0]);
            b2.putString("weatherInfo", weatherInfo[1]);
            b3.putString("weatherInfo", weatherInfo[2]);
            b4.putString("weatherInfo", weatherInfo[3]);
            b5.putString("weatherInfo", weatherInfo[4]);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //为ViewPage建立适配 以及生成五个Fragement对象
        fragList = new ArrayList<Fragment>();
        WeatherFragment wf1 = new WeatherFragment();
        WeatherFragment wf2 = new WeatherFragment();
        WeatherFragment wf3 = new WeatherFragment();
        WeatherFragment wf4 = new WeatherFragment();
        WeatherFragment wf5 = new WeatherFragment();
        //Bundle绑定Fragment
        wf1.setArguments(b1);
        wf2.setArguments(b2);
        wf3.setArguments(b3);
        wf4.setArguments(b4);
        wf5.setArguments(b5);
        //添加fragment到List
        fragList.add(wf1);
        fragList.add(wf2);
        fragList.add(wf3);
        fragList.add(wf4);
        fragList.add(wf5);
        //建立ViewPager适配器
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(this, fragList);
        //ViewPager绑定适配器
        ViewPager2 pager = findViewById(R.id.pager);
        pager.setAdapter(adapter);
    }

    //跳转到选择城市界面
    public void onClickChangeCity(View view) {
        Intent intent = new Intent(this, AddCityActivity.class);
        intent.putExtra("cityName", cityName);
        startActivity(intent);
    }
}
